package com.dys.dao.impl;

import com.dys.dao.GoodsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @className: com.dys.dao.impl.GoodsDaoImpl
 * @description: com.dys.dao.impl
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/10/21 22:15
 */
@Repository
public class GoodsDaoImpl implements GoodsDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int findPriceById(int goodsId) {
        String sql = "select price from t_goods where id=?";
        Integer price = jdbcTemplate.queryForObject(sql, Integer.class, goodsId);
        return price;
    }

    @Override
    public int updateGoodsStock(int goodsId) {
        String sql = "update t_goods set stock = stock - 1 where id = ?";
        int count = jdbcTemplate.update(sql, goodsId);
        return count;
    }

    @Override
    public int updateUserBalance(int userId, int price) {
        String sql = "update t_user set balance = balance - ? where id=?";
        int count = jdbcTemplate.update(sql, price, userId);
        return count;
    }
}
